package dao.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import dao.tDeviceDao;
import entity.tDevice;

public class tDeviceDaoImpl extends HibernateDaoSupport implements tDeviceDao {

	public void add(tDevice tdevice) {
		// TODO Auto-generated method stub
		
		this.getHibernateTemplate().save(tdevice);

	}

	public void delete() {
		// TODO Auto-generated method stub
		tDevice tdevice = new tDevice();
		tdevice.setDeviceId("56w363qw");
		System.out.println(tdevice);
		
		this.getHibernateTemplate().delete(tdevice);
		

	}

	@SuppressWarnings("unchecked")
	public List<tDevice> search() {
		// TODO Auto-generated method stub
		
		String hql = "from tDevice";
		return this.getHibernateTemplate().find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<tDevice> searchById(String deviceId){
		
		String hql = "from tDevice where deviceId = "+"'" + deviceId + "'"+"";
		
		return this.getHibernateTemplate().find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<tDevice> searchBymac(String mac){
		
		String hql = "from tDevice where mac = "+"'" + mac + "'"+"";
		
		return this.getHibernateTemplate().find(hql);
		
	}
	
	@SuppressWarnings("unchecked")
	public List<tDevice> searchBydeviceNo(String searchvalue,int pageSize,int page){
		
		String hql = "from tDevice where deviceNo like "+"'%" + searchvalue + "%'"+"";
		
		return this.getHibernateTemplate().find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<tDevice> searchdeviceNo(String deviceNo){
		String hql = "from tDevice where deviceNo = "+"'" + deviceNo + "'"+"";
		
		return this.getHibernateTemplate().find(hql);
		
	}
	
	@SuppressWarnings("unchecked")
	public List<tDevice> searchByuserName(String searchvalue){
		String hql = "from tUser where userName = "+"'" + searchvalue + "'"+"";
		return this.getHibernateTemplate().find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<tDevice> searchBytDeviceArea(String searchvalue){
		
		String hql = "from tDevice where deviceArea = "+"'" + searchvalue + "'"+"";
		
		return this.getHibernateTemplate().find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<tDevice> searchAlltDevice(){
		
		String hql = "from tDevice order by addTime desc";
		
		return this.getHibernateTemplate().find(hql);
	}

	public void update(tDevice tdevice) {
		// TODO Auto-generated method stub
		System.out.println(tdevice);
		
		this.getHibernateTemplate().update(tdevice);

	}
	

	@SuppressWarnings("unchecked")
	public long getCount(String hql){
		List list = this.getHibernateTemplate().find(hql);
		if (list != null) {
			return (Long) list.get(0);
		}
		return 0;
	}
	
	@SuppressWarnings("unchecked")
	public List<tDevice> searchByIndustry(String industry){
		String hql = "from tDevice where industry = "+"'" + industry + "'"+"";
		
		return this.getHibernateTemplate().find(hql);
		
	}
	
	@SuppressWarnings("unchecked")
	public List<tDevice> seatchdeviceNamevalue(String values){
		String hql = "from tDevice where deviceName like "+"'%" + values + "%'"+" or deviceNo like "+"'%" + values + "%'"+"";
		
		return this.getHibernateTemplate().find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<tDevice> seatchdeviceNovalue(String values){
		String hql = "from tDevice where deviceNo like "+"'%" + values + "%'"+"";
		
		return this.getHibernateTemplate().find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<tDevice> searchBytArea(String tAreaId,int pageSize,int page){
		
		String hql = "from tDevice where deviceArea = "+"'" + tAreaId + "'"+"";
		
		return this.getHibernateTemplate().find(hql);
	}
	
	public List queryForPage(final String hql,final int offset,final int length){
        List list = getHibernateTemplate().executeFind(new HibernateCallback(){
            public Object doInHibernate(Session session) throws HibernateException,SQLException{
                Query query = session.createQuery(hql);
                query.setFirstResult(offset);
                query.setMaxResults(length);
                List list = query.list();
                System.out.println(query.list());
                return list;
            }
        });
        return list;
    }
	
	 public int getAllRowCount(String hql){
	        return getHibernateTemplate().find(hql).size();
	    }



}
